package com.shaozi.nc.server.rpc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shaozi.data.rpc.base.RpcResultBean;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.resp.ReqListSecretaryBean;
import com.shaozi.nc.bean.resp.SecretaryMsgBean;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.data.model.SecretaryResponseModel;
import com.shaozi.nc.manage.secretary.BaseSecretaryManage;
import com.shaozi.nc.manage.secretary.SecretaryManage;
import com.shaozi.nc.utils.SecretaryHelper;

@Component("secretaryRpcServer")
public class SecretaryRpcServer extends BaseSecretaryRpcServer<SecretaryModel> {

	@Autowired
	private SecretaryManage secretaryMgr;

	public SecretaryRpcServer() {

	}

	@Override
	public BaseSecretaryManage<SecretaryModel> onManange() {
		return secretaryMgr;
	}

	@Override
	public RpcResultBean listUpdate(ReqListSecretaryBean reqBean) {
		RpcResultBean ret = new RpcResultBean();
		SecretaryResponseModel model = new SecretaryResponseModel();
		List<SecretaryModel> secretarys = null;
		boolean isInit = (reqBean.getTimestamp() == null) || (reqBean.getTimestamp() == 0l);
		try {
			secretarys = onManange().listUpdateObjects(reqBean);
			setBaseSecretaryResponseModel(model, isInit, secretarys, reqBean);
		} catch (SzException e) {
			ret.setCode(e.getCode());
			ret.setMsg(e.getMessage());
			return ret;
		}
		if ((secretarys == null) || (secretarys.isEmpty())) {
			return ret;
		}

		List<SecretaryMsgBean> msgBeans = new ArrayList<>();
		secretarys.forEach((secretary) -> {
			msgBeans.addAll(SecretaryHelper.secretaryModelToMsgBean(secretary));
		});
		model.setData(msgBeans);
		ret.setData(model);
		return ret;
	}

}
